package com.example.leetcode.queue.common;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

public class QueueUtils {
    public static int matrixSum(int[][] nums) {
        int row = nums.length;
        int col = nums[0].length;
        List<PriorityQueue<Integer>> queues = new ArrayList<>();
        for (int i = 0; i < row; i++) {
            queues.add(new PriorityQueue<>(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o2 - o1;
                }
            }));
        }
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < nums[i].length; j++) {
                queues.get(i).add(nums[i][j]);
            }
        }
        int ans = 0;
        for (int i = 0; i < col; i++) {
            int max = Integer.MIN_VALUE;
            for (int j = 0; j < row; j++) {
                Integer head = queues.get(j).poll();
                if (head > max) {
                    max = head;
                }
            }
            ans += max;
        }
        return ans;
    }
}
